﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BPM.ViewModel;
using BPM.Web;
using BPM.Service;
using BPM.Common;

namespace BPM.Controllers
{
    public class LogonController : BaseController
    {
        private UserService _uService;

        public LogonController()
        {
            _uService = new UserService();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Logon()
        {
			//return this.Logon(new LogonViewModel()
			//{
			//    UserName = "kg",
			//    Password = "1"
			//});
			return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Logon(LogonViewModel logonViewModel)
        {
            BPM.Entity.User currentUser = null;
            Result result = _uService.Logon(logonViewModel.UserName, logonViewModel.Password, out currentUser);

            if (result.Success)
            {
                this.CurrentUser = currentUser;
                if (currentUser.IsValid)
                {
                    return RedirectToAction(ad => ad.InternalDefaultView.Default);
                    //return RedirectToAction(AD.InternalDefaultView.Default, AD.InternalDefaultView);
                    //return RedirectToAction<InternalDefaultViewController>(c => c.Default());
                }
                else
                {
                    return View(AD.Singleton.Logon.WaitVerification);
                    //return View<LogonController>(c => c.WaitVerification());
                }
            }
            else
            {
                logonViewModel.Message = result;
                return View(logonViewModel);
            }
        }

        public ActionResult Logout()
        {
            this.CurrentUser = null;
            return RedirectToAction(a => a.Logon.Logon);
        }

        /// <summary>
        /// 当前帐号还未通过经理审核
        /// </summary>
        /// <returns></returns>
        public ActionResult WaitVerification()
        {
            return View();
        }
    }

    public class LogonControllerActions : ControllerDefinition<LogonController>
    {
        public readonly string Logon = "Logon";
		public readonly string Logout = "Logout";
		public readonly string WaitVerification = "WaitVerification";
    }
}